﻿using System;
using System.Xml.Linq;

namespace YArchitech.LIB
{
	public static class XElementExtersion
	{
		public static int GetInt(this XElement xe, string attribute, int def = 0)
		{
			int result;
			try
			{
				result = (int)xe.Attribute(attribute);
			}
			catch
			{
				result = def;
			}
			return result;
		}

		public static double GetDouble(this XElement xe, string attribute, double def = 0.0)
		{
			double result;
			try
			{
				result = (double)xe.Attribute(attribute);
			}
			catch
			{
				result = def;
			}
			return result;
		}

		public static bool GetBool(this XElement xe, string attribute, bool def = false)
		{
			bool result;
			try
			{
				result = (bool)xe.Attribute(attribute);
			}
			catch
			{
				result = def;
			}
			return result;
		}

		public static string GetString(this XElement xe, string attribute, string def = "")
		{
			string result;
			try
			{
				result = xe.Attribute(attribute).Value;
			}
			catch
			{
				result = def;
			}
			return result;
		}

		public static XElement ChildElement(this XElement xe, string ChildElementName)
		{
			XElement xelement = xe.Element(ChildElementName);
			if (xelement == null)
			{
				xelement = new XElement(ChildElementName);
				xe.Add(xelement);
			}
			return xelement;
		}
	}
}
